/**
 * Date Service
 * 日期服务， 提供对日期的相关操作
 */

angular.module('NxtMap')
  .service('DateService', DateService);

function DateService() {

  //获取当前月第一天到最后一天
  this.getCurrentMonthSE = function() {
    var today = new Date();
    today.setDate(1);
    var start = today.format('yyyy-MM-dd');
    today.setMonth(today.getMonth() + 1);
    today.setDate(today.getDate() - 1);
    var end = today.format('yyyy-MM-dd');
    return [start, end];
  }

  //获取当前星期的第一天到最后一天
  //星期日为起始日
  this.getCurrentWeekSE = function() {
    var today = new Date();
    today.setDate(today.getDate() - today.getDay());
    var start = today.format('yyyy-MM-dd');
    today.setDate(today.getDate() + 6);
    var end = today.format('yyyy-MM-dd');
    return [start, end];
  }
  
}

Date.prototype.format = function (fmt) { //author: meizz 
  var o = {
      "M+": this.getMonth() + 1, //月份 
      "d+": this.getDate(), //日 
      "H+": this.getHours(), //小时 
      "m+": this.getMinutes(), //分 
      "s+": this.getSeconds(), //秒 
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
      "w+": this.getDay(), //星期，0为周日，1为周一，依次。。。
      "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}